package com.cdh.edu.controller.admin;


import com.cdh.edu.entity.Video;
import com.cdh.edu.service.VideoService;
import com.cdh.utils.result.Restful;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author cdh
 * @since 2020-10-20
 */
@ApiModel(description = "小节管理")
@RestController
@RequestMapping("video")
@CrossOrigin
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 添加小节
     * @param video
     * @return
     */
    @PostMapping("add")
    public Restful addVideo(@RequestBody Video video){
        boolean save = videoService.save(video);
        if (save){
            return Restful.ok();
        }else {
            return Restful.error().message("保存小节失败");
        }
    }

    /**
     * 删除小节同时把视频也删除
     * @param videoId
     * @return
     */
    @DeleteMapping("delete/{videoId}")
    public Restful deleteVideo(@PathVariable("videoId") String videoId){
        // 此处调用VOD中的删除视频文件的接口
        videoService.removeVideo(videoId);
        boolean isOk = videoService.removeById(videoId);
        if (isOk){
            return Restful.ok();
        }else {
            return Restful.error().message("删除小节失败");
        }
    }

    /**
     * 修改小节
     * @param video
     * @return
     */
    @PostMapping("update")
    public Restful updateVideo(@RequestBody Video video){
        boolean update = videoService.updateById(video);
        if (update){
            return Restful.ok();
        }else {
            return Restful.error().message("修改小节失败");
        }
    }

    /**
     * 查询小节
     * @param videoId
     * @return
     */
    @GetMapping("query/{videoId}")
    public Restful queryVideo(@PathVariable("videoId") String videoId){
        Video video = videoService.getById(videoId);
        if (video != null){
            return Restful.ok().data("video",video);
        }else {
            return Restful.error().message("不存在该视频");
        }
    }



}

